'''
Created on 03.06.2012

@author: johannes
'''

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from JsonHandlerGAE import JsonHandler



class Note(db.Model):
        content = db.StringProperty(multiline=True)
        date = db.DateTimeProperty(auto_now_add=True)

class MyJsonHandler(JsonHandler):

    def get_notes(self):
        notes = db.GqlQuery("SELECT * FROM Note ORDER BY date DESC LIMIT 10")
        notes = [{'content':note.content,
                            'date':str(note.date)} for note in notes]
        return notes

    def post_note(self, content):
        note = Note()
        note.content = content
        note.put()
        return self.get_notes()

    def get_source(self):
        return ['## code.py\n\n' + 
            file('code.py').read() + 
            '\n\n## JsonHandlerGAE.py\n\n' + 
            file('JsonHandlerGAE.py').read() + 
            '\n\n## app.yaml\n\n' + 
            file('app.yaml').read() +
            '\n\n## client.py\n\n' + 
            file('client.py').read() + 
            '\n\n## BackendProxy.py\n\n' + 
            file('BackendProxy.py').read()]


application = webapp.WSGIApplication(
        [('/json', MyJsonHandler)],
        debug=True)
        
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()